import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.DefaultHandler;

/**
 * 使用SAX操作xml的简单例子
 * @author Administrator
 *
 */
public class SAXOperateXmlDemo {
    public void parseXml01(){
        String xmlPath = "D:\\project\\dynamicWeb\\src\\resource\\user01.xml";  
        String xmlName = xmlPath.substring(xmlPath.lastIndexOf("\\"));
        try {
            //获取SAX分析器的工厂实例，专门负责创建SAXParser分析器
            SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
            //获取SAXParser分析器的实例
            SAXParser saxParser = saxParserFactory.newSAXParser();
            //和其他解析方式一样，也要间接通过InputStream输入流对象获取xml信息
            //1、直接指定绝对路径获取文件输入流对象
            //InputStream inputStream = new FileInputStream(xmlPath);
            //2、使用类的相对路径查找xml路径
            //InputStream inputStream = this.getClass().getResourceAsStream(xmlName);
            //3、也可以指定路径完成InputStream输入流的实例化操作  
            InputStream inputStream = new FileInputStream(new File(xmlPath));  
            //4、使用InputSource输入源作为参数也可以转换xml  
            //InputSource inputSource = new InputSource(inputStream);
            
            //解析xml文档
            saxParser.parse(inputStream, new XmlSAXHandler01());//这里传递了自定义的XmlSAXHandler()管理者参数来解析xml,不像以前都是直接调用返回的Document对象
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SAXOperateXmlDemo demo = new SAXOperateXmlDemo();
        demo.parseXml01();
    }
}

/**
 * 解析SAX的处理者01
 * @author Administrator
 *
 */
class XmlSAXHandler01 extends DefaultHandler {  
    
    @Override
    public void startDocument() throws SAXException {
        System.out.println("---->startDocument() is invoked...");
        super.startDocument();
    }
    
    @Override
    public void endDocument() throws SAXException {
        System.out.println("---->endDocument() is invoked...");
        super.endDocument();
    }
    
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        System.out.println("-------->startElement() is invoked...");
        super.startElement(uri, localName, qName, attributes);
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        System.out.println("-------->endElement() is invoked...");
        super.endElement(uri, localName, qName);
    }
    
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        System.out.println("------------>characters() is invoked...");
        super.characters(ch, start, length);
    }
}